package com.x.wx.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.x.framework.dao.BaseDao;
import com.x.wx.model.WxKeyword;

@Repository
public class WxKeywordDao extends BaseDao{
	
	private final static  String  selectWxKeywordListSql = "SELECT T1.* FROM WX_KEYWORD T1 WHERE 1=1 ";
	
	/**
	 * 查询关键字
	 * @param wxKeyword
	 * @return
	 * @throws Exception
	 */
	public List<WxKeyword> selectWxKeywordList(WxKeyword wxKeyword) throws Exception{
		StringBuilder sql = new StringBuilder(selectWxKeywordListSql);
        List<Object> params = new ArrayList<Object>();
        searchCondition(sql, params, wxKeyword);
		return this.queryForList(sql.toString(), WxKeyword.class, params, wxKeyword);
	}
	
	private final static  String  selectWxKeywordCountSql = "SELECT COUNT(T1.KEYWORD_ID) FROM WX_KEYWORD T1 WHERE 1=1 ";
	
	/**
	 * 查询关键字条数
	 * @param wxKeyword
	 * @return
	 * @throws Exception
	 */
	public int selectWxKeywordNum(WxKeyword wxKeyword) throws Exception{
        StringBuilder sql = new StringBuilder(selectWxKeywordCountSql);
        List<Object> params = new ArrayList<Object>();
        searchCondition(sql, params, wxKeyword);
        return this.queryForInt(sql.toString(), params);
    }
	
	/**
	 * 查询条件
	 * @param sql
	 * @param params
	 * @param wxKeyword
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, WxKeyword wxKeyword) {
        if(wxKeyword != null) {
            if(StringUtils.isNotEmpty(wxKeyword.getKeywordId())){
                sql.append("AND T1.KEYWORD_ID = ? ");
                params.add(wxKeyword.getKeywordId());
            }
            if(StringUtils.isNotEmpty(wxKeyword.getKeywordName())){
                sql.append("AND T1.KEYWORD_NAME = ? ");
                params.add(wxKeyword.getKeywordName());
            }
            if(StringUtils.isNotEmpty(wxKeyword.getKeywordType())){
                sql.append("AND T1.KEYWORD_TYPE = ? ");
                params.add(wxKeyword.getKeywordType());
            }
            if(StringUtils.isNotEmpty(wxKeyword.getKeywordValue())){
                sql.append("AND T1.KEYWORD_VALUE = ? ");
                params.add(wxKeyword.getKeywordValue());
            }
	        if(StringUtils.isNotEmpty(wxKeyword.getAccountId())){
		        sql.append("AND T1.ACCOUNT_ID = ? ");
		        params.add(wxKeyword.getAccountId());
	        }
        }
    }

}
